var Index = require('../app/controllers/index');
var User = require('../app/controllers/user');
var Movie = require('../app/controllers/movie');
var Comment = require('../app/controllers/comment');
var Category = require('../app/controllers/category');

module.exports = function (app) {
  // --------------------MIDWARE-------------------------
  // 自定义中间件 - 所有GET请求的预处理, 以便每个页面都可以使用会话中的user数据
  app.use(function (req, res, next) {
    var _user = req.session.user;
    res.locals.user = _user;
    next();
  });
  // --------------------INDEX-------------------------
  app.get('/', Index.show); // 首页 - 显示全部分类
  app.get('/results', Index.search); // 首页 - 按分类查询显示
  // --------------------USER--------------------------
  // page
  app.get('/signup', User.showSignup);
  app.get('/signin', User.showSignin);
  app.get('/admin/user/list', User.signinRequired, User.superAdminRequired, User.list);
  // action
  app.post('/user/signup', User.signup);
  app.post('/user/signin', User.signin);
  app.get('/logout', User.logout);
  // ---------------------MOVIE------------------------
  // page
  app.get('/movie/:id', Movie.detail);
  app.get('/admin/movie/new', User.signinRequired, User.adminRequired, Movie.new);
  app.get('/admin/movie/update/:id', User.signinRequired, User.adminRequired, Movie.update);
  app.get('/admin/movie/list', User.signinRequired, User.adminRequired, Movie.list);
  // action
  app.post('/admin/movie', User.signinRequired, User.adminRequired, Movie.savePoster, Movie.save);
  app.delete('/admin/movie/list', User.signinRequired, User.superAdminRequired, Movie.del);
  // ---------------------COMMENT--------------------------
  app.post('/user/comment', User.signinRequired, Comment.save);
  // -------------------CATEGORY------------------------
  // page
  app.get('/admin/category/new',  User.signinRequired, User.adminRequired, Category.new);
  app.get('/admin/category/list', User.signinRequired, User.adminRequired, Category.list);
  // action
  app.post('/admin/category', User.signinRequired, User.adminRequired, Category.save);
  app.delete('/admin/category/list', User.signinRequired, User.superAdminRequired, Category.del);
};
